/**
 * 
 */

"use strict";

var animation = animation || {};

animation.animations = [];
animation.animLastUpdate = 0;
animation.pause = false;

animation.animUpdate = function(timeStamp) {

	var timeSpan = timeStamp - animation.animLastUpdate;

	if (animation.animLastUpdate && !animation.pause) {

		if (timeSpan > 2000) {
			// time out
			console
					.log('animation.animUpdate() --> animation watchdog, skip animation step with a time span of '
							+ timeSpan + 'ms');
		} else if (timeSpan >= 10) {
			// each 10 ms scan animations
			var instances = [];
			animation.animations.foreach(function(i, value, first, last) {
				instances.push(value);
			});
			instances.foreach(function(i, value, first, last) {
				// animation frequency 0.25s
				if (value.clock.ton(250))
					value.step();
			});
		} else
			return;
	}
	// remind time stamp
	animation.animLastUpdate = timeStamp;

};

animation.steam = function(ref_field, vec2_offset) {
	if (!ref_field instanceof fields.Field)
		throw new Error('argument field must be instanceof fields.Field');
	fields.Field.call(this, ref_field.column, ref_field.row, 'steam');
	this.clock = new utils.Clock();
	var vec2;
	if (!vec2_offset instanceof utils.Vec2)
		vec2 = ref_field.vec2;
	else
		vec2 = ref_field.vec2.add(vec2_offset);
	this.setInnerPos(vec2);
	this.stepinc = 0;
};
animation.steam.prototype = new fields.Field();
animation.steam.prototype.place = function() {
	fields.FieldPosition.prototype.place.call(this);
	fields.el.append(this.el);
	this.id = fields.ID++;
	fields.by_id[this.id] = this;
	this.clock.reset();
	var el = this.el;
	el.removeClass();
	this.cls.foreach(function(i, value, first, last) {
		el.addClass(value);
	});
	this.stepinc = 0;
	animation.animations.push(this);
	$(this.el).fadeIn(500);
	this.step();
};
animation.steam.prototype.remove = function() {
	fields.Field.prototype.remove.call(this);
	animation.animations.remove(this);
};
animation.steam.prototype.step = function() {
	if (this.placed()) {
		if (this.stepinc >= 4) {
			$(this.el).fadeOut(750, (function() {
				var anim = this;
				return function() {
					anim.remove();
				};
			}).call(this));
			return false;
		}
		this.el.removeClass('s' + this.stepinc);
		++this.stepinc;
		this.el.addClass('s' + this.stepinc);
		return true;
	} else {
		this.remove();
		return false;
	}
};

animation.explosion = function(ref_field, vec2_offset) {
	if (!ref_field instanceof fields.Field)
		throw new Error('argument field must be instanceof fields.Field');
	fields.Field.call(this, ref_field.column, ref_field.row, 'collision');
	this.clock = new utils.Clock();
	var vec2;
	if (!vec2_offset instanceof utils.Vec2)
		vec2 = ref_field.vec2;
	else
		vec2 = ref_field.vec2.add(vec2_offset);
	this.setInnerPos(vec2);
	this.stepinc = 0;
};
animation.explosion.prototype = new fields.Field();
animation.explosion.prototype.place = function() {
	fields.FieldPosition.prototype.place.call(this);
	fields.el.append(this.el);
	this.id = fields.ID++;
	fields.by_id[this.id] = this;
	this.clock.reset();
	var el = this.el;
	el.removeClass();
	this.cls.foreach(function(i, value, first, last) {
		el.addClass(value);
	});
	this.stepinc = 0;
	animation.animations.push(this);
	$(this.el).fadeIn(100);
	this.step();
};
animation.explosion.prototype.remove = function() {
	fields.Field.prototype.remove.call(this);
	animation.animations.remove(this);
};
animation.explosion.prototype.step = function() {
	if (this.placed()) {
		if (this.stepinc >= 7) {
			$(this.el).fadeOut(750, (function() {
				var anim = this;
				return function() {
					anim.remove();
				};
			}).call(this));
			return false;
		}
		this.el.removeClass('s' + this.stepinc);
		++this.stepinc;
		this.el.addClass('s' + this.stepinc);
		return true;
	} else {
		this.remove();
		return false;
	}
};
